
const {ccclass, property} = cc._decorator;

type heroState = {
    stand: number,
    attck: number,
    hurt: number
}
@ccclass
export default class Enemy extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    // 血量
    hp:number = 5;
    // 是否被攻击到
    isHit:boolean = false;
    // 动画
    aniEnemy:cc.Animation;

    // 敌人状态
    private State:heroState = {
        stand: 1,
        attck: 2,
        hurt: 3
    }
    // 敌人刚体对象
    rb:cc.RigidBody ;
    // 站立状态
    enemyState:number = this.State.stand;
    // 人物方向
    sp:cc.Vec2 = cc.v2(0, 0);
    lv:cc.Vec2 = cc.v2(0, 0);
    // 当前动作
    curAni: string = ''
    // 人物速度
    @property(cc.Integer)
    speed:number = 0;
    // 设置总时间
    tt: number = 0;
    // 移动是否向左
    moveLeft: boolean = false;
    moveRight: boolean = false;

    // 拿到玩家node
    playerNode:cc.Node;

    onLoad () {
        this.aniEnemy = this.node.getChildByName("body").getComponent(cc.Animation);
        this.aniEnemy.on('finished', this.onAnimaFinished, this);
        this.rb = this.node.getComponent(cc.RigidBody);

        this.playerNode = cc.find('Canvas/bg/Hero');
    }
    // 受伤动画结束
    onAnimaFinished(event:cc.Event) {
        // 判断当前动画
        let name = this.aniEnemy.currentClip.name;
        if (name == 'hurt') {
            this.hp --;
            if (this.hp == 0) {
                this.node.destroy();
            } else {
                this.enemyState = 1;
                
            }
        } else if (name == 'attack') {
            this.enemyState = 1;
            this.setAni('idle');
        }
    }

    // 被攻击到
    hurt() {
        this.isHit = true;
        this.enemyState = 3;
        
        this.lv = this.rb.linearVelocity;
        this.lv.x = 0;
        this.rb.linearVelocity = this.lv;

        this.setAni('hurt');
    }

    // 敌人行为变化
    enemyAction(dt:number) {
        // 获取玩家位置
        let h_pos:cc.Vec3 = this.playerNode.position;
        // 自己的位置
        let e_pos:cc.Vec3 = this.node.position;
        // 计算距离
        let dis = cc.Vec3.distance(h_pos, e_pos);

        let v = h_pos.sub(e_pos);

        if (dis <= 60) {
            // 攻击
            console.log('攻击');
            if (v.x < 0) { 
                this.node.scaleX = Math.abs(this.node.scaleX)
            } else {
                this.node.scaleX = -Math.abs(this.node.scaleX)
            }
            this.attack();
        } else if (dis > 60 && dis <= 150) {
            // 追击
            
            if (v.x < 0) {
                this.moveLeft = true;
                this.moveRight = false;
            } else {
                this.moveLeft = false;
                this.moveRight = true;
            }
            this.enemyState = 1;
        } else {
            // 不动
            this.enemyState = 1;
        }
    }
    // 攻击
    attack() {
        this.setAni('attack');
        // 停止移动
        this.lv = this.rb.linearVelocity;
        this.lv.x = 0;
        this.rb.linearVelocity = this.lv; 
        this.moveLeft = false;
        this.moveRight = false;
        // 状态变成攻击
        this.enemyState = 2;
    }
    // 设置动画事件
    setAni(anima:string) {
        if (this.curAni == anima) return;
        this.curAni = anima;
        this.aniEnemy.play(this.curAni)
    }
    // 移动
    move() {
        if (this.enemyState != 1) return ;
        let scaleX = Math.abs(this.node.scaleX);
        this.lv = this.rb.linearVelocity;
        if (this.moveLeft) {
            this.sp.x = -1;
            this.node.scaleX = scaleX;
            this.setAni('enemyRun')
        } else if (this.moveRight) {
            this.sp.x = 1;
            this.node.scaleX = -scaleX;
            this.setAni('enemyRun')
        } else {
            this.sp.x = 0;
            this.setAni('idle')
        }  

        if (this.sp.x != 0) {
            this.lv.x = this.sp.x * this.speed;
        } else {
            this.lv.x = 0
        }
        // console.log(this.lv.x)
        this.rb.linearVelocity = this.lv;
    }

    update (dt:number) {
        // 状态切换
        this.tt += dt;
        if (this.tt >= 0.3 && this.enemyState == 1) {
            this.enemyAction(dt);
            this.tt = 0;
        }
        if (this.enemyState == 2) {
            this.attack();
        } else if (this.enemyState == 1) {
            this.move();
        }
        
    }
}
